package test;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class PinyinUtils {
    /**
     * 获取字符串拼音的第一个字母
     *
     * @param chinese
     * @return
     */
    public static String toFirstChar(String chinese) {
        StringBuilder pinyinStr = new StringBuilder();
        char[] newChar = chinese.toCharArray();  //转为单个字符
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (char c : newChar) {
            if (c > 128) {
                try {
                    System.out.println("newChar[i]: " + c);
                    Boolean aBoolean = cheackStr(String.valueOf(c));
                    System.out.println("是否是特殊字符:" + aBoolean);
                    if (!aBoolean) {
                        char c1 = PinyinHelper.toHanyuPinyinStringArray(c, defaultFormat)[0].charAt(0);
                        System.out.println("转换后： " + c1);
                        pinyinStr.append(c1);
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pinyinStr.append(c);
            }
        }
        return pinyinStr.toString();
    }

    /**
     * 汉字转为拼音
     *
     * @param chinese
     * @return
     */
    public static String toPinyin(String chinese) {
        String pinyinStr = "";
        char[] newChar = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    System.out.println("newChar[i]" + newChar[i]);
                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pinyinStr += newChar[i];
            }
        }
        return pinyinStr;
    }

    /**
     * 汉字转为拼音并获取大写的首字母;
     *
     * @param chinese
     * @return
     */
    public static String toPinyinAndGetFirstChar(String chinese) {
        StringBuilder pinyinStr = new StringBuilder();
        char[] newChar = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    Boolean aBoolean = cheackStr(String.valueOf(newChar[i]));
                    if (!aBoolean) {
                        char s = PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0);
                        pinyinStr.append(s);
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pinyinStr.append(newChar[i]);
            }
        }
//        String cases = pinyinStr.toString().toUpperCase();
//        System.out.println("Case : " + cases);
        return pinyinStr.toString().toUpperCase();
    }

    // 比较字符串
    public static int compareString(String o1, String o2) {
        if (o1 == null && o2 == null) {
            return 0;
        } else if (o1 == null) {
            return 1; // null 值排在最后面
        } else if (o2 == null) {
            return -1; // null 值排在最后面
        }

        String newo1;
        newo1 = toPinyinAndGetFirstChar(o1);
        String newo2;
        newo2 = toPinyinAndGetFirstChar(o2);
//        return newo1.substring(0, 1).compareTo(newo2.substring(0, 1));


        return compareChar(newo1.toCharArray(), newo2.toCharArray(), 0);
    }

    public static Boolean cheackStr(String str) {
        String illegal = "`~!#%^&@*=+\\|{};:'\",<>/?○●★☆☉♀♂※¤╬の〆()（）！~@#￥……&1234567890-";
        return illegal.contains(str);
    }

    // 递归比较字符数组的对应位置上的字符是否相等
    private static int compareChar(char[] c1, char[] c2, int index) {
        if (index >= c1.length && index >= c2.length) {
            // 比较到末尾都未发现字符不相等，返回true
            return 0;
        }
        if (index >= c1.length) { //c1 长度小 等价于 x1(阿多多h) < x2(阿多多) ? = fales x2放前面  (阿多多h本来在前面)
            return -1;
        }
        if (index >= c2.length) {// c2长度长 等价于 x1 < x2 ? = true x1放前面
            return 1;
        }

        String s1 = String.valueOf(c1[index]);
        String s2 = String.valueOf(c2[index]);
        if (s1.compareTo(s2) != 0) {
            return s1.compareTo(s2);
        }
        return compareChar(c1, c2, index + 1);
    }


}
